package com.sch.www.ele.services.impl;


import com.sch.www.codeGenerator.base.BaseServicesSimpleImp;
import com.sch.www.ele.entity.CareMemberToElevator;
import com.sch.www.ele.entity.CareMemberToElevatorExample;
import com.sch.www.ele.mapper.CareMemberToElevatorMapper;
import com.sch.www.ele.services.CareMemberServices;
import com.sch.www.ele.services.CareMemberToElevatorServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CareMemberToElevatorServicesImpl extends BaseServicesSimpleImp<CareMemberToElevator, CareMemberToElevatorExample, CareMemberToElevatorMapper, CareMemberToElevatorServices> implements CareMemberToElevatorServices {
    @Autowired
    CareMemberServices careMemberServices;

    @Override
    protected CareMemberToElevatorExample getExampleNewInstance() {
        return new CareMemberToElevatorExample();
    }

    @Override
    protected String getTableName() {
        return null;
    }

    @Override
    public List<String> selectMemberIdByElevatorId(String... elevatorIds) {
        if (isNull(elevatorIds)) {
            return new ArrayList<>();
        } else {
            CareMemberToElevatorExample example = getExampleNewInstance();
            if (elevatorIds.length == 1) {
                example.createCriteria().andElevatorIdEqualTo(elevatorIds[0]);
            } else {
                example.createCriteria().andElevatorIdIn(Arrays.asList(elevatorIds));
            }
            List<CareMemberToElevator> elevatorLinkMemberList = getMapper().selectByExample(example);
            List<String> memberIdList = elevatorLinkMemberList.stream().map(item -> item.getMemberId()).collect(Collectors.toList());
            return memberIdList;
        }
    }

    @Override
    public List<String> selectElevatorIdByMemberId(String... memberIds) {
        if (isNull(memberIds)) {
            return new ArrayList<>();
        } else {
            CareMemberToElevatorExample example = getExampleNewInstance();
            if (memberIds.length == 1) {
                example.createCriteria().andMemberIdEqualTo(memberIds[0]);
            } else {
                example.createCriteria().andMemberIdIn(Arrays.asList(memberIds));
            }
            List<CareMemberToElevator> elevatorLinkMemberList = getMapper().selectByExample(example);
            List<String> elevatorIdList = elevatorLinkMemberList.stream().map(item -> item.getElevatorId()).collect(Collectors.toList());
            return elevatorIdList;
        }
    }

    @Override
    public int deleteByMemberId(String... memberIds) {
        if (isNull(memberIds)) {
            return 0;
        } else {
            CareMemberToElevatorExample example = getExampleNewInstance();
            if (memberIds.length == 1) {
                example.createCriteria().andMemberIdEqualTo(memberIds[0]);
            } else {
                example.createCriteria().andMemberIdIn(Arrays.asList(memberIds));
            }
            int result = getMapper().deleteByExample(example);
            return result;
        }
    }


    @Transactional
    @Override
    public void insert(String memberId, List<String> elevatorIdList) {
        if (careMemberServices.selectById(memberId) == null) {
            throw new IllegalArgumentException("不存memberId为" + memberId);
        }
        getCurrentProxy().deleteByMemberId(memberId);
        if (!CollectionUtils.isEmpty(elevatorIdList)) {

            List<CareMemberToElevator> insertList = new ArrayList<>();
            elevatorIdList.forEach(elevatorId -> {
                CareMemberToElevator elevatorLinkMember = new CareMemberToElevator();
                elevatorLinkMember.setElevatorId(elevatorId);
                elevatorLinkMember.setMemberId(memberId);
                insertList.add(elevatorLinkMember);
            });
            getCurrentProxy().insertSelective(insertList.toArray(new CareMemberToElevator[]{}));
        }
    }
}
